home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / tracker-4.13.lha / tracker / Arch / Sgi / audio.c next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  3.8 KB  |  186 lines

  1. /* sgi/audio.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: audio.c,v 4.2 1995/02/01 16:43:47 espie Exp $
  6.  * $Log: audio.c,v $
  7.  * Revision 4.2  1995/02/01  16:43:47  espie
  8.  * 23 bit samples.
  9.  *
  10.  * Revision 4.2  1995/02/01  16:43:47  espie
  11.  * 23 bit samples.
  12.  *
  13.  * Revision 4.0  1994/01/11  18:01:04  espie
  14.  * Changed name.
  15.  *
  16.  * Revision 1.1  1993/12/26  00:55:53  Espie
  17.  * Initial revision
  18.  *
  19.  * Revision 3.8  1993/12/04  16:12:50  espie
  20.  * BOOL -> boolean.
  21.  *
  22.  * Revision 3.7  1993/11/11  20:00:03  espie
  23.  * Amiga support.
  24.  *
  25.  * Revision 3.6  1993/07/14  16:33:41  espie
  26.  * Added stuff.
  27.  *
  28.  * Revision 3.5  1993/05/09  14:06:03  espie
  29.  * Corrected mix problem.
  30.  *
  31.  * Revision 3.4  1992/11/27  10:29:00  espie
  32.  * General cleanup
  33.  *
  34.  * Revision 3.3  1992/11/24  10:51:19  espie
  35.  * Added pseudo discardbuffer.
  36.  *
  37.  * Revision 3.2  1992/11/22  17:20:01  espie
  38.  * Checks for finetune ?
  39.  *
  40.  * Revision 3.1  1992/11/19  20:44:47  espie
  41.  * Protracker commands.
  42.  *
  43.  * Revision 3.0  1992/11/18  16:08:05  espie
  44.  * New release.
  45.  *
  46.  * Revision 2.11  1992/11/17  15:38:00  espie
  47.  * Dummy discard_buffer()
  48.  * Changed sync_audio value again.
  49.  * Added synchro for dump.
  50.  * Bug fix: must ask new frequency after we tried to set it to get it
  51.  * rounded up.
  52.  * Added stereo option (kind of).
  53.  * Separated mix/stereo stuff.
  54.  * Checked buffer size.
  55.  * Added possibility to get back to MONO for the sgi.
  56.  * Added stereo capabilities to the indigo version.
  57.  * Ask the frequency to the audio device.
  58.  * Corrected bug: when closing audio,
  59.  * we now wait for the samples queue to be empty.
  60.  */
  61.  
  62. #include <audio.h>
  63. #include "defs.h"
  64. #include "extern.h"
  65.  
  66. XT int sginap(long ticks);
  67.      
  68. ID("$Id: audio.c,v 4.2 1995/02/01 16:43:47 espie Exp $")
  69.  
  70. LOCAL signed short *buffer;
  71. LOCAL int index;
  72.  
  73. LOCAL int number;
  74. LOCAL int sync = FALSE;
  75.  
  76. LOCAL ALport audio;
  77. LOCAL ALconfig config;
  78.  
  79. LOCAL int donotwait = FALSE;
  80. LOCAL long chpars[] = {AL_OUTPUT_RATE, 0};
  81.  
  82. LOCAL int stereo;  /* are we playing stereo or not ? */
  83. /* 256th of primary/secondary source for that side. */
  84. LOCAL int primary, secondary, scale;
  85.  
  86. void set_mix(percent)
  87. int percent;
  88.     {
  89.     percent *= 256;
  90.     percent /= 100;
  91.     primary = percent;
  92.     secondary = 512 - percent;
  93.      scale = 65536;
  94.     }
  95.  
  96. int open_audio(f, s)
  97. int f, s;
  98.     {
  99.  
  100.     donotwait = FALSE;
  101.     chpars[1] = f;
  102.     if (f != 0)
  103.         ALsetparams(AL_DEFAULT_DEVICE, chpars, 2);
  104.     ALgetparams(AL_DEFAULT_DEVICE, chpars, 2);
  105.     config = ALnewconfig();
  106.     stereo = s;
  107.     if (stereo)
  108.         {
  109.         ALsetchannels(config, AL_STEREO);
  110.         number = 2;
  111.         }
  112.     else
  113.         {
  114.         ALsetchannels(config, AL_MONO);
  115.         number = 1;
  116.         }
  117.     ALsetwidth(config, AL_SAMPLE_16);
  118.     audio = ALopenport("soundtracker mono", "w", config);
  119.     index = 0;
  120.     buffer = malloc(sizeof(signed short) * number * chpars[1]);
  121.     return chpars[1];
  122.     }
  123.  
  124. void set_synchro(s)
  125. int s;
  126.     {
  127.     sync = s;
  128.     }
  129.  
  130. int update_frequency()
  131.     {
  132.     int oldfreq;
  133.  
  134.     oldfreq = chpars[1];
  135.     ALgetparams(AL_DEFAULT_DEVICE, chpars, 2);
  136.     if (chpars[1] != oldfreq)
  137.         {
  138.         buffer = realloc(buffer, sizeof(signed short) * number * chpars[1]);
  139.         return chpars[1];
  140.         }
  141.     else
  142.         return 0;
  143.     }
  144.  
  145.  
  146. void output_samples(int left, int right)
  147.     {
  148.     if (stereo)
  149.         {
  150.         buffer[index++] = (left * primary + right * secondary)/scale;
  151.         buffer[index++] = (right * primary + left * secondary)/scale;
  152.         }
  153.     else
  154.         buffer[index++] = (left + right)/256;
  155.     }
  156.  
  157. void flush_buffer(void)
  158.     {
  159.     ALwritesamps(audio, buffer, index);
  160.     if (sync)
  161.         while(ALgetfilled(audio) > index * 10)
  162.             /* busy wait */
  163.             ;
  164.     index = 0;
  165.     }
  166.  
  167. void discard_buffer(void)
  168.     {
  169.     donotwait = TRUE;
  170.     /* mostly not implemented, only working when using close_audio
  171.      * right after
  172.      */
  173.     }
  174.  
  175. void close_audio(void)
  176.     {
  177.     if (!donotwait)
  178.         {
  179.         while(ALgetfilled(audio) != 0)
  180.             sginap(1);
  181.         }
  182.     ALcloseport(audio);
  183.     ALfreeconfig(config);
  184.     free(buffer);
  185.     }
  186.